using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using ScenarioFramework.WorkflowEngine.DataModel.Common;

namespace ScenarioFramework.Modeler.Forms.BPMN
{
	/// <summary>
	/// Descrizione di riepilogo per FormAttributes.
	/// </summary>
	public class ExtendedAttributes : System.Windows.Forms.Form
	{
        XMLExtendedAttributes xmlAttributes;

		private System.Windows.Forms.ListView listAttributes;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Button btnName;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem addToolStripMenuItem;
        private ToolStripMenuItem modifyToolStripMenuItem;
        private ToolStripMenuItem removeToolStripMenuItem;
        private IContainer components;

        public ScenarioFramework.Modeler.Forms.BPMN.FormExit Exit = FormExit.SAVE;

		public ExtendedAttributes()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
            xmlAttributes = new XMLExtendedAttributes();

			this.BuildGridHeader();
		}

		public ExtendedAttributes(string sAttributes) : this()
		{
			if ( sAttributes != "" )
			{
				xmlAttributes.Parse ( sAttributes);
				this.FillForm ();
			}
		}

		public ExtendedAttributes(XMLExtendedAttributes vAttributes) : this()
		{
			if ( vAttributes != null )
			{
				xmlAttributes = vAttributes;

				this.FillForm ();
			}
		}

        public XMLExtendedAttributes XMLExtendedAttributes
		{
			get
			{
				return xmlAttributes;
			}
		}

		private void BuildGridHeader()
		{
			listAttributes.Clear();

            listAttributes.Columns.Add("Name", listAttributes.Width/2,  HorizontalAlignment.Left);
			listAttributes.Columns.Add ("Value", listAttributes.Width/2, HorizontalAlignment.Left);
		}

		private void FillForm()
		{
			this.BuildGridHeader();
			foreach ( XMLExtendedAttribute xmlAttribute in xmlAttributes )
			{
				ListViewItem item = new ListViewItem( new string[] { xmlAttribute.Name, xmlAttribute.Value});
				listAttributes.Items.Add (item);
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.listAttributes = new System.Windows.Forms.ListView();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.addToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.modifyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnName = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.contextMenuStrip1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // listAttributes
            // 
            this.listAttributes.ContextMenuStrip = this.contextMenuStrip1;
            this.listAttributes.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listAttributes.Location = new System.Drawing.Point(0, 0);
            this.listAttributes.Name = "listAttributes";
            this.listAttributes.Size = new System.Drawing.Size(707, 393);
            this.listAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.listAttributes.TabIndex = 0;
            this.listAttributes.UseCompatibleStateImageBehavior = false;
            this.listAttributes.View = System.Windows.Forms.View.Details;
            this.listAttributes.DoubleClick += new System.EventHandler(this.listAttributes_DoubleClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.addToolStripMenuItem,
            this.modifyToolStripMenuItem,
            this.removeToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(125, 70);
            // 
            // addToolStripMenuItem
            // 
            this.addToolStripMenuItem.Name = "addToolStripMenuItem";
            this.addToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.addToolStripMenuItem.Text = "Add";
            this.addToolStripMenuItem.Click += new System.EventHandler(this.addToolStripMenuItem_Click);
            // 
            // modifyToolStripMenuItem
            // 
            this.modifyToolStripMenuItem.Name = "modifyToolStripMenuItem";
            this.modifyToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.modifyToolStripMenuItem.Text = "Modify";
            this.modifyToolStripMenuItem.Click += new System.EventHandler(this.modifyToolStripMenuItem_Click);
            // 
            // removeToolStripMenuItem
            // 
            this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
            this.removeToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
            this.removeToolStripMenuItem.Text = "Remove";
            this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnName);
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 334);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(707, 59);
            this.panel2.TabIndex = 15;
            // 
            // btnName
            // 
            this.btnName.Location = new System.Drawing.Point(635, 12);
            this.btnName.Name = "btnName";
            this.btnName.Size = new System.Drawing.Size(60, 35);
            this.btnName.TabIndex = 12;
            this.btnName.Text = "XML";
            this.btnName.Click += new System.EventHandler(this.btnName_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(12, 12);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 35);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(569, 12);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(60, 35);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // ExtendedAttributes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(707, 393);
            this.ControlBox = false;
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.listAttributes);
            this.Name = "ExtendedAttributes";
            this.Text = "Extended Attributes";
            this.contextMenuStrip1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

        private void BuildXMLExtendedAttributes()
        {
            xmlAttributes.Clear();

            for (int i = 0; i < listAttributes.Items.Count; i++)
            {
                ListViewItem item = listAttributes.Items[i];
                XMLExtendedAttribute xmlAttribute = new XMLExtendedAttribute(item.SubItems[0].Text, item.SubItems[1].Text);
                xmlAttributes.Add(xmlAttribute);
            }
        }

		private void btnSave_Click(object sender, System.EventArgs e)
		{
            this.BuildXMLExtendedAttributes();

            this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
            Exit = FormExit.CANCEL;
			this.Close();
		}

		private void listAttributes_DoubleClick(object sender, System.EventArgs e)
		{
            this.modifyToolStripMenuItem_Click(listAttributes, null);
		}

		private void btnName_Click(object sender, System.EventArgs e)
		{
            Scenario.Modeler.FormXML frmXML = new Scenario.Modeler.FormXML();
            this.BuildXMLExtendedAttributes();
			frmXML.XML = xmlAttributes.GetXML(true, 0);
			frmXML.ShowDialog(this);

			if ( frmXML.OK )
			{
				xmlAttributes.Clear();
				xmlAttributes.Parse ( frmXML.XML);
			}
		}

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExtendedAttribute frmAttribute = new  ExtendedAttribute();
            frmAttribute.ShowDialog(this);

            if (frmAttribute.Exit == FormExit.SAVE )
            {
                ListViewItem item = new ListViewItem(new string[] { frmAttribute.XMLExtendedAttribute.Name, frmAttribute.XMLExtendedAttribute.Value });
                listAttributes.Items.Add(item);
                listAttributes.Refresh();
            }
        }

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listAttributes.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select one attribute.");
                return;
            }
            else if (listAttributes.SelectedItems.Count > 1)
            {
                MessageBox.Show("Select only one attribute.");
                return;
            }

            ListViewItem item = listAttributes.SelectedItems[0];
            XMLExtendedAttribute xmlAttribute = new XMLExtendedAttribute(item.SubItems[0].Text, item.SubItems[1].Text);

            ExtendedAttribute frmAttribute = new ExtendedAttribute(xmlAttribute);
            frmAttribute.ShowDialog();

            if (frmAttribute.Exit == FormExit.SAVE )
            {
                listAttributes.SelectedItems[0].SubItems[0].Text = frmAttribute.xmlAttribute.Name;
                listAttributes.SelectedItems[0].SubItems[1].Text = frmAttribute.xmlAttribute.Value;
            }
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listAttributes.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select one attribute.");
                return;
            }

            string sMessage = string.Empty;
            foreach (ListViewItem item in listAttributes.SelectedItems)
            {
                listAttributes.Items.Remove (item);
            }
        }
	}
}
